var util = require("../../../utils/util.js"),
{api} = require("../../../utils/api.js"),
    app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    bannerList: [],
    goods_id:'',
    goods_type:0,
    goodsNum:1,
    storeList:[],
    detail:null,
    cartNums:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({goods_id:options.id})
    this.getUserSetting()
    this.cartList();
    this.getUsers();
  },
  getUserSetting() {
    let that = this;
    wx.getSetting({
        success: (res) => {
            if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
                wx.showModal({
                    title: '请求授权当前位置',
                    content: '需要获取您的地理位置，请确认授权',
                    success: function (res) {
                        if (res.cancel) {
                            wx.showToast({
                                title: '拒绝授权',
                                icon: 'none',
                                duration: 1000
                            })
                            that.goodsDetail()
                        } else if (res.confirm) {
                            wx.openSetting({
                                success: function (dataAu) {
                                    if (dataAu.authSetting["scope.userLocation"] == true) {
                                        wx.showToast({
                                            title: '授权成功',
                                            icon: 'success',
                                            duration: 1000
                                        })
                                        that.getLocationAddress();
                                    } else {
                                        wx.showToast({
                                            title: '授权失败',
                                            icon: 'none',
                                            duration: 1000
                                        })
                                        that.goodsDetail();
                                    }
                                },
                                fail:function(){
                                  that.goodsDetail();
                                }
                            })
                        }
                    }
                })
            } else {
                that.getLocationAddress();
            }
        },
        fail:function(){
          that.goodsDetail();
        }
    })
},
getLocationAddress() {
  let positionUpdateTime=wx.getStorageSync('positionUpdateTime');
  if(positionUpdateTime && positionUpdateTime<new Date().getTime()){
      this.goodsDetail();
      return false;
  }
    let that = this;
    wx.getLocation({
        type: 'wgs84',
        success(res) {
            console.log('wgs84', res)
            const {
                latitude,
                longitude
            } = res;
            if (latitude && longitude) {
                wx.setStorageSync('latitude', latitude);
                wx.setStorageSync('longitude', longitude);
                wx.setStorageSync('positionUpdateTime', (new Date().getTime()+14400000));
                that.goodsDetail();
            } else {
                that.goodsDetail()
            }
        },
        fail: function () {
            that.goodsDetail()
        }
    })
},
  async getUsers() {
    const data = await app.getUsers();
    data && this.setData(data)
},
  gotoPage(e){
    const {url}=e.currentTarget.dataset;
    wx.navigateTo({url});
  },
  async cartList(){
    let {data} = await api.cartList();
    this.setData({
      cartNums:data && Array.isArray(data.goods_list)?data.goods_list.length:0
    })
  },
  async goodsDetail(){
    let lat=wx.getStorageSync('latitude');
    let lng=wx.getStorageSync('longitude');
    let {data} = await api.goodsDetail(this.data.goods_id,lat,lng);
    this.setData({
      storeList:data && data.goods && Array.isArray(data.goods.storeList)?data.goods.storeList:[],
      detail:data && data.goods?data.goods:null,
      bannerList:data && data.goods && Array.isArray(data.goods.banner) && data.goods.banner.length>0?data.goods.banner:(data.goods.image?[data.goods.image]:[]),
      goods_type:data && data.goods && data.goods.type==0?1:2 
    })
  },
  goHome(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  goCard(){
    wx.switchTab({
      url: '/pages/shopping/cart/cart',
    })
  },
  goAddCard(){
    if (util.getUserInfo(this.data.userInfo, true) == false)return false;
    this.setData({
      playType:1,
      // show:true,
      goodsNum:1
    })
    this.agreementSubmit();
  },
  goPlay(){
    if (util.getUserInfo(this.data.userInfo, true) == false)return false;
    this.setData({
      playType:2,
      // show:true,
      goodsNum:1
    })
    this.agreementSubmit();
  },
  jian(e){
    this.setData({goodsNum:e.detail})
  },
  agreementSubmit(){
    const {playType,goods_id,goods_type}=this.data;
    if(playType == 1){
      api.addCart(goods_id).then(res=>{
        if(res.code == 1){
          this.setData({show:false});
          this.cartList();
          wx.showToast({
            title: '添加成功',
            icon:'success'
          })
        }
      })
    }else{
      wx.setStorageSync('orderGoodsList', [this.data.detail])
      wx.navigateTo({
        url: `/pages/shopping/goodsSubmit/goodsSubmit?goods_type=${goods_type}&goods_id=${goods_id}`,
      })
    }
  },
  goKf(){
    app.getKf()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return {
      title: this.data.detail.title,
      imageUrl: this.data.detail.goods.image, 
      path: '/pages/shopping/goodsDetail/goodsDetail?id=' + this.data.goods_id + "&type=" +this.data.goods_type
    }
  }
})